package com.tech.connect.api;

import android.text.TextUtils;

import com.ksy.common.api.BaseOb;
import com.tech.connect.activity.BaseActivity;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.Disposable;

/**
 * Created by Ksy
 */

public abstract class BaseEntityOb<T> extends BaseOb<BaseEntity<T>> {
    private WeakReference<BaseActivity> activityWeakReference;

    public BaseEntityOb() {
    }

    public BaseEntityOb(BaseActivity activity) {
        activityWeakReference = new WeakReference<>(activity);
    }

    @Override
    public void onSubscribe(Disposable disposable) {
        super.onSubscribe(disposable);
        if (activityWeakReference != null) {
            BaseActivity baseActivity = activityWeakReference.get();
            if (baseActivity != null) {
                baseActivity.addDisposable(disposable);
            }
        }
    }

    public abstract void onDataDeal(boolean isSuccess, T data, String msg);

    @Override
    public void onDataDeal(BaseEntity<T> data) {
        if (data != null) {
            //0 表示请求成功 -1 为有必选参数为空 -2 为登录用户信息验证不通过
            String msg = data.message;
            if (TextUtils.equals("SUCCESS", data.code)) {
                //状态码成功
                if (TextUtils.isEmpty(msg)) {
                    msg = "请求成功";
                }
                onDataDeal(true, data.data, msg);
//            } else if (data.code == -1) {
//                if (TextUtils.isEmpty(msg)) {
//                    msg = "请求失败";
//                }
//                onDataDeal(false, data.data, msg);
//            } else if (data.code == -2) {
//                if (TextUtils.isEmpty(msg)) {
//                    msg = "用户信息验证不通过";
//                }
//                onDataDeal(false, data.data, msg);
            } else {
                if (TextUtils.isEmpty(msg)) {
                    msg = "请求出现错误";
                }
                onDataDeal(false, null, msg);
            }
        } else {
            //请求失败
            onDataDeal(false, null, "请求出现错误");
        }
    }
}
